﻿Imports System.Data.SqlClient
Imports NENAData.DLC
Imports NENAData.Transactions
Imports NENAData.NENAMember
Imports Secure.Functions

Partial Public Class Update
    Inherits System.Web.UI.Page

    Private Sub Page_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        'only allow the correct affiliate to view
        Dim strAffiliateID As String

        strAffiliateID = Request.QueryString("AffiliateID")
        'verify user is accessing an affiliate they have rights to
        'VerifyAffiliate(strAffiliateID)

        If Not IsPostBack Then
            Load_ENAODistricts_DDL()
            'load only data from the specified affiliate
            If strAffiliateID = "" Then
                txtAffiliate.Text = 0
            Else
                txtAffiliate.Text = strAffiliateID
            End If
            'lblTitle.Text &= txtAffiliate.Text

            BindDataList(txtAffiliate.Text)
        End If

    End Sub

    Private Sub TransactionList()
        'Dim oPayments As New NENAData.Transactions.Payments.Payments

        'GridView1.DataSource = oPayments.GetTransactionPayments(CType(txtMemberID.Text, Integer))
        'GridView1.DataBind()
        'DetailsView1

    End Sub
    Private Sub Load_ENAODistricts_DDL()

        Dim x As ENAODistricts = New ENAODistricts

        ddl_ENAODistricts.DataSource = x.GetDistricts_All()
        ddl_ENAODistricts.DataValueField = "District_ID"
        ddl_ENAODistricts.DataTextField = "District_Description" '"Province_ID"
        ddl_ENAODistricts.DataBind()
        ddl_ENAODistricts.Items.Insert(0, "--Select--")
    End Sub

    Private Sub BindDataList(ByVal strAffiliateID As String)
        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim dtrTitles As SqlDataReader
        Dim strSql As String

        strSql = "select lastname + ', ' + firstname as fullname, lastname, member_id from members " & _
        " where members.affiliate_id = " & txtAffiliate.Text & _
        " order by lastName;"
        '" and Account_Status > '" & ConfigurationManager.AppSettings.Item("MemberStatusPending") & "' " & _

        conPubs = New SqlConnection(ConfigurationManager.AppSettings.Item("SQLConnectionString"))
        cmdSelect = New SqlCommand(strSql, conPubs)
        conPubs.Open()
        dtrTitles = cmdSelect.ExecuteReader()

        ddlLoadMember.DataSource = dtrTitles
        ddlLoadMember.DataTextField = ("FullName")
        ddlLoadMember.DataValueField = ("member_id")
        ddlLoadMember.DataBind()

        dtrTitles.Close()
        conPubs.Close()
    End Sub

    Private Sub BindMemberData()
        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand
        Dim dtrTitles As SqlDataReader
        Dim strSql, strMemberID As String

        strMemberID = ddlLoadMember.SelectedValue.ToString

        strSql = "select * from members, affiliate where " & _
       "members.affiliate_id = affiliate.affiliate_id " & _
       " and members.member_id =" & strMemberID

        conPubs = New SqlConnection(ConfigurationManager.AppSettings.Item("SQLConnectionString"))
        cmdSelect = New SqlCommand(strSql, conPubs)
        conPubs.Open()
        dtrTitles = cmdSelect.ExecuteReader()

        While dtrTitles.Read
            PersonalFirstName.Text = dtrTitles("FirstName")
            PersonalLastName.Text = dtrTitles("LastName")
            PersonalEmail.Text = dtrTitles("emailaddress")
            PersonalHomePhone.Text = dtrTitles("homephone")
            PersonalAddress1.Text = dtrTitles("address")
            PersonalAddress2.Text = dtrTitles("address2")
            PersonalCity.Text = dtrTitles("city")
            PersonalPostalCode.Text = dtrTitles("postalcode")
            PersonalProvince.Text = dtrTitles("province")
            txtOtherCountry.Text = dtrTitles("country")
            txtWorkPhone.Text = dtrTitles("workphone")
            txtEmployer.Text = dtrTitles("employer")
            txtPaymentDate.Text = dtrTitles("Date_Paid")
            'PersonalFirstName.Text = dtrTitles("FirstName")
            lblAcctStatusID.Text = ReturnStatus(dtrTitles("Account_Status"))

            chkENPCProvider.Checked = ReturnChecked(dtrTitles("enpc_provider"))
            chkENPCInstructor.Checked = ReturnChecked(dtrTitles("enpc_instructor"))
            chkENPCTrainer.Checked = ReturnChecked(dtrTitles("enpc_course_trainer"))
            chkENPCDirector.Checked = ReturnChecked(dtrTitles("enpc_course_director"))
            txtENPCID.Text = dtrTitles("enpc_trainer_id")

            chkPCTASProvider.Checked = ReturnChecked(dtrTitles("pctas_provider"))
            chkPCTASInstructor.Checked = ReturnChecked(dtrTitles("pctas_instructor"))
            chkPCTASTrainer.Checked = ReturnChecked(dtrTitles("pctas_course_trainer"))
            chkPCTASDirector.Checked = ReturnChecked(dtrTitles("pctas_course_director"))

            chkACTASProvider.Checked = ReturnChecked(dtrTitles("actas_provider"))
            chkACTASInstructor.Checked = ReturnChecked(dtrTitles("actas_instructor"))
            chkACTASTrainer.Checked = ReturnChecked(dtrTitles("actas_course_trainer"))
            chkACTASDirector.Checked = ReturnChecked(dtrTitles("actas_course_director"))

            chkCCTASTrainer.Checked = ReturnChecked(dtrTitles("CCTAS_Provider"))
            chkCCTASProvider.Checked = ReturnChecked(dtrTitles("cctas_Trainer"))

            chkCATNProvider.Checked = ReturnChecked(dtrTitles("catn_provider"))
            chkCATNInstructor.Checked = ReturnChecked(dtrTitles("catn_instructor"))
            chkCATNTrainer.Checked = ReturnChecked(dtrTitles("catn_course_trainer"))
            chkCATNDirector.Checked = ReturnChecked(dtrTitles("catn_course_director"))

            chkTNCCProvider.Checked = ReturnChecked(dtrTitles("tncc_provider"))
            chkTNCCInstructor.Checked = ReturnChecked(dtrTitles("tncc_instructor"))
            chkTNCCTrainer.Checked = ReturnChecked(dtrTitles("tncc_course_trainer"))
            chkTNCCDirector.Checked = ReturnChecked(dtrTitles("tncc_course_director"))
            txtTNCCID.Text = dtrTitles("tncc_trainer_id")

            chkENCCCertified.Checked = ReturnChecked(dtrTitles("encc_certified"))
            chkEmployeeAdministration.Checked = ReturnChecked(dtrTitles("aoe_administration"))
            chkEmployeeClinical.Checked = ReturnChecked(dtrTitles("aoe_clinical"))
            chkEmployeeEducator.Checked = ReturnChecked(dtrTitles("aoe_educator"))
            chkEmployeeResearch.Checked = ReturnChecked(dtrTitles("aoe_research"))

            txtMemberID.Text = dtrTitles("member_id")
            txtNurseLicense.Text = dtrTitles("Nurses_License_ID")
            txtEmployer.Text = dtrTitles("Employer")

            txtAffiliateID.Text = dtrTitles("Affiliate_ID")

            If txtAffiliateID.Text = "5" Then
                lbl_ENAODistricts.Visible = True
                ddl_ENAODistricts.Visible = True
                check_RNAO.Visible = True
            Else
                lbl_ENAODistricts.Visible = False
                ddl_ENAODistricts.Visible = False
                check_RNAO.Visible = False
            End If

            'If txtAffiliateID.Text = "5" Then
            '    ddl_ENAODistricts.Visible = True
            '    check_RNAO.Visible = True
            'End If

            check_RNAO.Checked = ReturnChecked(dtrTitles("RNAO"))
            ddl_ENAODistricts.SelectedIndex = dtrTitles("ENAODistrict_ID")
            txtMemberReferralCode.Text = dtrTitles("Referral_Code")

            If txtMemberReferralCode.Text.Length < 2 Then
                'All paid members get a referral code created
                btnGenerateReferralCode.Enabled = True
            End If

        End While
        dtrTitles.Close()
        conPubs.Close()

    End Sub

    Private Sub btnSave_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnSave.Click

        If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleNoSave")) Then
            Response.Redirect("default.aspx")
        End If

        Dim strFirstName, strLastName, strEmail, strHomePhone As String
        Dim strAddress1, strAddress2, strCity, strPostalCode As String
        Dim strAffiliateId, strEmployer As String
        Dim strProvince, strCountry, strWorkPhone As String
        Dim strAdministration, strClinical, strEducator, strResearch As String
        Dim strPCTASProvider, strPCTASInstructor, strPCTASTrainer, strPCTASDirector As String
        Dim strACTASProvider, strACTASInstructor, strACTASTrainer, strACTASDirector As String
        Dim strCCTASProvider, strCCTASTrainer As String
        Dim strENPCProvider, strENPCInstructor, strENPCTrainer, strENPCDirector As String
        Dim strCATNProvider, strCATNInstructor, strCATNTrainer, strCATNDirector As String
        Dim strTNCCProvider, strTNCCInstructor, strTNCCTrainer, strTNCCDirector As String
        Dim strSQL, strENCCCertified, strNursesLicenseID As String
        Dim strENPCID, strTNCCID, strENAOID, strRNAO As String

        If txtMemberID.Text = "0" Then
            Exit Sub
        End If

        strENPCID = SqlReplaceText(txtENPCID.Text)
        strTNCCID = SqlReplaceText(txtTNCCID.Text)

        strFirstName = SqlReplaceText(PersonalFirstName.Text)
        strLastName = SqlReplaceText(PersonalLastName.Text)
        strEmail = SqlReplaceText(PersonalEmail.Text)
        strHomePhone = SqlReplaceText(PersonalHomePhone.Text)
        strAddress1 = SqlReplaceText(PersonalAddress1.Text)
        strAddress2 = SqlReplaceText(PersonalAddress2.Text)
        strCity = SqlReplaceText(PersonalCity.Text)
        strPostalCode = SqlReplaceText(PersonalPostalCode.Text)
        strAffiliateId = ddlAffiliate.SelectedValue.ToString
        strProvince = SqlReplaceText(PersonalProvince.Text)
        strCountry = SqlReplaceText(txtOtherCountry.Text)
        strWorkPhone = SqlReplaceText(txtWorkPhone.Text)
        strEmployer = SqlReplaceText(txtEmployer.Text)

        strAdministration = EvaluateChecked(chkEmployeeAdministration.Checked)
        strClinical = EvaluateChecked(chkEmployeeClinical.Checked)
        strEducator = EvaluateChecked(chkEmployeeEducator.Checked)
        strResearch = EvaluateChecked(chkEmployeeResearch.Checked)

        strENCCCertified = EvaluateChecked(chkENCCCertified.Checked)

        strPCTASProvider = EvaluateChecked(chkPCTASProvider.Checked)
        strPCTASInstructor = EvaluateChecked(chkPCTASInstructor.Checked)
        strPCTASTrainer = EvaluateChecked(chkPCTASTrainer.Checked)
        strPCTASDirector = EvaluateChecked(chkPCTASDirector.Checked)

        strACTASProvider = EvaluateChecked(chkACTASProvider.Checked)
        strACTASInstructor = EvaluateChecked(chkACTASInstructor.Checked)
        strACTASTrainer = EvaluateChecked(chkACTASTrainer.Checked)
        strACTASDirector = EvaluateChecked(chkACTASDirector.Checked)

        strCCTASProvider = EvaluateChecked(chkCCTASProvider.Checked)
        strCCTASTrainer = EvaluateChecked(chkACTASTrainer.Checked)

        strENPCProvider = EvaluateChecked(chkENPCProvider.Checked)
        strENPCInstructor = EvaluateChecked(chkENPCInstructor.Checked)
        strENPCTrainer = EvaluateChecked(chkENPCTrainer.Checked)
        strENPCDirector = EvaluateChecked(chkENPCDirector.Checked)

        strCATNProvider = EvaluateChecked(chkCATNProvider.Checked)
        strCATNInstructor = EvaluateChecked(chkCATNInstructor.Checked)
        strCATNTrainer = EvaluateChecked(chkCATNTrainer.Checked)
        strCATNDirector = EvaluateChecked(chkCATNDirector.Checked)

        strTNCCProvider = EvaluateChecked(chkTNCCProvider.Checked)
        strTNCCInstructor = EvaluateChecked(chkTNCCInstructor.Checked)
        strTNCCTrainer = EvaluateChecked(chkTNCCTrainer.Checked)
        strTNCCDirector = EvaluateChecked(chkTNCCDirector.Checked)

        strRNAO = EvaluateChecked(check_RNAO.Checked)

        strNursesLicenseID = txtNurseLicense.Text
        strENAOID = ddl_ENAODistricts.SelectedIndex.ToString

        strSQL = "update members set " & _
            "lastname = '" & strLastName & "', " & _
            "firstname = '" & strFirstName & "', " & _
            "address = '" & strAddress1 & "', " & _
            "address2 = '" & strAddress2 & "', " & _
            "city = '" & strCity & "', " & _
            "postalcode = '" & strPostalCode & "', " & _
            "country = '" & strCountry & "', "
        strSQL &= "homephone = '" & strHomePhone & "', " & _
            "workphone = '" & strWorkPhone & "', " & _
            "emailaddress = '" & strEmail & "', " & _
            "aoe_Administration = '" & strAdministration & "', " & _
            "aoe_clinical = '" & strClinical & "', " & _
            "aoe_educator = '" & strEducator & "', " & _
            "aoe_research = '" & strResearch & "', " & _
            "ENCC_Certified = '" & strENCCCertified & "', "
        strSQL &= "tncc_provider = '" & strTNCCProvider & "', " & _
            "tncc_instructor = '" & strTNCCInstructor & "', " & _
            "tncc_course_trainer = '" & strTNCCTrainer & "', " & _
            "tncc_course_director = '" & strTNCCDirector & "', " & _
            "enpc_provider = '" & strENPCProvider & "', " & _
            "enpc_instructor = '" & strENPCInstructor & "', " & _
            "enpc_course_trainer = '" & strENPCTrainer & "', " & _
            "enpc_course_director = '" & strENPCDirector & "', " & _
            "catn_provider = '" & strCATNProvider & "', " & _
            "catn_instructor = '" & strCATNInstructor & "', " & _
            "catn_course_trainer = '" & strCATNTrainer & "', " & _
            "catn_course_director = '" & strCATNDirector & "', "
        strSQL &= "pctas_provider = '" & strPCTASProvider & "', " & _
            "pctas_instructor = '" & strPCTASInstructor & "', " & _
            "pctas_course_trainer = '" & strPCTASTrainer & "', " & _
            "pctas_course_director = '" & strPCTASDirector & "', "
        strSQL &= "actas_provider = '" & strACTASProvider & "', " & _
            "actas_instructor = '" & strACTASInstructor & "', " & _
            "actas_course_trainer = '" & strACTASTrainer & "', " & _
            "actas_course_director = '" & strACTASDirector & "', " & _
            "cctas_provider = '" & strCCTASProvider & "', " & _
            "cctas_trainer = '" & strCCTASTrainer & "', "
        strSQL &= "nurses_license_id = '" & strNursesLicenseID & "', " & _
            "enpc_trainer_id = '" & strENPCID & "', " & _
            "tncc_trainer_id = '" & strTNCCID & "', " & _
            "rnao = '" & strRNAO & "', " & _
            "ENAODistrict_ID = '" & strENAOID & "', " & _
            "employer = '" & strEmployer & "' " & _
            "where member_id =" & txtMemberID.Text & ";"

        'Response.Write("strSQL: " & strSQL)
        Dim conPubs As SqlConnection
        Dim cmdSelect As SqlCommand

        conPubs = New SqlConnection(ConfigurationManager.AppSettings.Item("SQLConnectionString"))
        cmdSelect = New SqlCommand(strSQL, conPubs)
        conPubs.Open()
        cmdSelect.ExecuteNonQuery()
        conPubs.Close()

        BindMemberData()

        'save email address to community server

    End Sub

    Private Function EvaluateChecked(ByVal strChecked) As String

        If strChecked = "false" Then
            Return ("N")
        Else
            Return ("Y")
        End If

    End Function

    Private Function ReturnChecked(ByVal strChecked) As Boolean

        If strChecked = "Y" Then
            Return True
        Else
            Return False
        End If

    End Function
    Private Sub btnLoadDemographics_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnLoadDemographics.Click

        BindMemberData()
        TransactionList()

        'enable the delete button when a member is loaded
        'btnDelete.Enabled = True
        btnSave.Enabled = True

    End Sub


#Region " Verifying Affiliate ID "

    Public Sub VerifyAffiliate(ByVal strAffiliateID As String)
        Select Case strAffiliateID
            Case 1
                If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleModerator")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleAdmin")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleBC")) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleBC")
                Else
                    Response.Redirect("")
                End If
            Case 2
                If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleModerator")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleAdmin")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleAB")) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleAB")
                Else
                    Response.Redirect("")
                End If
            Case 3
                If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleModerator")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleAdmin")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleSK")) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleSK")
                Else
                    Response.Redirect("")
                End If
            Case 4
                If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleModerator")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleAdmin")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleMB")) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleMB")
                Else
                    Response.Redirect("")
                End If
            Case 5
                If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleModerator")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleAdmin")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleONT")) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleONT")
                Else
                    Response.Redirect("")
                End If
            Case 6
                If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleModerator")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleAdmin")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleNB")) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleNB")
                Else
                    Response.Redirect("")
                End If
            Case 7
                If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleModerator")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleAdmin")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleNS")) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleNS")
                Else
                    Response.Redirect("")
                End If
            Case 8
                If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleModerator")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleAdmin")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RolePEI")) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RolePEI")
                Else
                    Response.Redirect("")
                End If
            Case 9
                If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleModerator")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleAdmin")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleNF")) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleNF")
                Else
                    Response.Redirect("")
                End If
            Case 10
                If User.IsInRole(ConfigurationManager.AppSettings.Item("RoleModerator")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleAdmin")) Or _
                    User.IsInRole(ConfigurationManager.AppSettings.Item("RoleOTHER")) Then
                    txtAffiliate.Text = ConfigurationManager.AppSettings.Item("RoleOTHER")
                Else
                    Response.Redirect("")
                End If
        End Select
    End Sub
#End Region
    Private Sub BtnReturn_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles BtnReturn.Click
        Response.Redirect("default.aspx")
    End Sub

    Function SqlReplaceText(ByVal strSql As String) As String
        Dim strFixedSQL As String

        strFixedSQL = Replace(strSql, "'", "")
        strFixedSQL = Replace(strFixedSQL, "[", "")
        strFixedSQL = Replace(strFixedSQL, "%", "")
        'strFixedSQL = Replace(strFixedSQL, "_", "[_]") 
        'some people may have underscores in their lastname
        strFixedSQL = Replace(strFixedSQL, "--", "")

        'return the fixed sql
        SqlReplaceText = strFixedSQL
    End Function
    Protected Sub btnMemberApply_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMemberApply.Click
        Dim oMember As New NENAMember
        oMember.UpdateMemberApplication(txtMemberID.Text, MemberStatus.Status.Pending)
        BindMemberData()
    End Sub
    Protected Sub btnMemberExpire_Click(ByVal sender As Object, ByVal e As EventArgs) Handles btnMemberExpire.Click
        Dim oMember As New NENAMember
        oMember.UpdateMemberApplication(txtMemberID.Text, MemberStatus.Status.Expired)
        BindMemberData()
    End Sub

    Protected Sub btnGenerateReferralCode_Click(sender As Object, e As EventArgs) Handles btnGenerateReferralCode.Click
        Dim oreferral = New Referral.Generate()
        oreferral.GenerateUnqiueMemberReferralCode(Convert.ToInt32(txtMemberID.Text))
        btnGenerateReferralCode.Enabled = False
    End Sub
End Class